package com.xiaoyu.tool.controller.client.wx.pay;

import com.xiaoyu.tool.controller.client.user.BaseUserController;
import com.xiaoyu.tool.entity.order.Order;
import com.xiaoyu.tool.enums.ResultEnum;
import com.xiaoyu.tool.response.ResultResponse;
import com.xiaoyu.tool.response.order.OrderNewRequest;
import com.xiaoyu.tool.service.order.OrderService;
import com.xiaoyu.tool.utils.ResultResponseUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.SortedMap;

/**
 * @author xiaoyu on 2018/11/01
 * 小程序原生支付
 */
@RestController
@RequestMapping("/wxPay")
@Slf4j
public class WxMaPayController extends BaseUserController {

    private final OrderService orderService;

    public WxMaPayController(OrderService orderService, WxMpService wxMpService) {
        super(wxMpService);
        this.orderService = orderService;
    }

    /**
     * 微信小程序支付
     * */
    @ApiOperation("微信小程序支付")
    @PostMapping("/newWxApplet")
    public ResultResponse newWxAppletOrder(@RequestBody OrderNewRequest orderNewRequest){
        //1.判断地址是否为空
        //2.判断库存是否充足
        //3.判断用户是否登录
//        String unionId = getCurrentUnionId();
        //TODO
        String unionId = "";
        if (StringUtils.isEmpty(unionId)){
            return ResultResponseUtil.error(ResultEnum.OBJECT_ERROR.getCode(),"获取用户unionId失败");
        }
        //4.创建订单记录
        Order order = orderService.createOrder(orderNewRequest,unionId);
        String ip = getUserIp();
        //5.获取唤起支付数据
        SortedMap<Object, Object> packageP = orderService.getPayInfo(order, unionId, ip);
        return ResultResponseUtil.success(packageP);
    }




}
